package com.rookie.data.generate.component.generate;

import com.rookie.data.generate.component.AbstractAnnotationGenerate;
import com.rookie.data.generate.constant.HighRateWord;
import com.rookie.data.generate.constant.annotation.CommonType;
import com.rookie.data.generate.model.project.TableColumnEntity;
import com.rookie.data.generate.util.GenerateUtils;
import com.rookie.data.generate.util.IdCardUtils;
import com.rookie.data.generate.util.IpUtils;
import com.rookie.data.generate.util.RegularUtils;
import com.rookie.data.generate.util.id.IdWorker;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.UUID;

/**
 * @author rookie
 */
@Component
public class CommonTypeGenerate extends AbstractAnnotationGenerate {

    public static String baseChar = "abcdefghijklmnopqrstuvwxyz0123456789";

    private static final String[] NET_SUFFIX = new String[]{".com",".cn",".net",".top","xyz",".icu",".shop",".vip"};

    private static final String[] TEL_FIRST = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");

    private static final String[] EMAIL_SUFFIX = "@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn".split(",");

    @Override
    public Object generate(TableColumnEntity table, Annotation annotation) {
        CommonType commonType = (CommonType) annotation;
        switch (commonType.type()) {
            case PHONE:
                return getPhone();
            case EMAIL:
                return getEmail();
            case UUID:
                return UUID.randomUUID().toString();
            case TIME_ID:
                return IdWorker.getTimeId();
            case UNIQUE_ID:
                return IdWorker.getId();
            case WEB_FILE:
                return generateFileUrl(commonType.value());
            case ID_CARD:
                return IdCardUtils.generate();
            case IMG_URL:
                return getImgUrl();
            case REGULAR:
                return RegularUtils.generate(commonType.value());
            case IP_ADDRESS:
                return IpUtils.getRandomIp();
            case CHINESE_NAME:
                return HighRateWord.STAR_NAME.get(GenerateUtils.random(0, HighRateWord.STAR_NAME.size()));
            default:
                return null;
        }
    }

    @Override
    public Class<? extends Annotation> getAnnotationClass() {
        return CommonType.class;
    }

    private static String getPhone() {
        int index = GenerateUtils.random(0, TEL_FIRST.length - 1);
        String first = TEL_FIRST[index];
        String second = String.valueOf(GenerateUtils.random(1, 888) + 10000).substring(1);
        String third = String.valueOf(GenerateUtils.random(1, 9100) + 10000).substring(1);
        return first + second + third;
    }

    public static String getEmail() {
        int length = 10;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = (int) (Math.random() * baseChar.length());
            sb.append(baseChar.charAt(number));
        }
        sb.append(EMAIL_SUFFIX[(int) (Math.random() * EMAIL_SUFFIX.length)]);
        return sb.toString();
    }

    public static String getFixChar(int size) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < size; i++) {
            result.append(baseChar.charAt(GenerateUtils.random(0,baseChar.length())));
        }
        return result.toString();
    }

    public static String getImgUrl() {
        return "https://www." +
                getFixChar(GenerateUtils.random(3, 6)) +
                NET_SUFFIX[GenerateUtils.random(0, NET_SUFFIX.length)]
                + "/" + UUID.randomUUID().toString() + ".png";
    }

    public static String generateFileUrl(String fileName) {
        return "https://www." +
                getFixChar(GenerateUtils.random(3, 6)) +
                NET_SUFFIX[GenerateUtils.random(0, NET_SUFFIX.length)]
                + "/" + UUID.randomUUID().toString() + "." + fileName;
    }

    public static void main(String[] args) {
        System.out.println(IdWorker.getId());
        System.out.println(IdWorker.getIdStr());
        System.out.println(IdWorker.getTimeId());
    }
}
